package com.ruoyi.system.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.Orders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.TbCustomerOrder;
import com.ruoyi.system.service.ITbCustomerOrderService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单管理Controller
 * 
 * @author ruoyi
 * @date 2024-09-12
 */
@RestController
@RequestMapping("/merchant_order")
public class TbCustomerOrderController extends BaseController
{
    @Autowired
    private ITbCustomerOrderService tbCustomerOrderService;


    /**
     * 新增
     */
    @PostMapping("/add")
    public String add(@RequestBody Orders orders) {


        String url = tbCustomerOrderService.add(orders);
        return url;

    }



    /**
     * 查询订单管理列表
     */
    @RequiresPermissions("system:merchant_order:list")
    @GetMapping("/list")
    public TableDataInfo list(TbCustomerOrder tbCustomerOrder)
    {


        try {

            // 设定日期格式
            SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");

            // 如果 orderTime 是 Date 类型，直接格式化
            if (tbCustomerOrder.getOrderTime() != null) {
                String formattedDate = outputFormat.format(tbCustomerOrder.getOrderTime());
                tbCustomerOrder.setTime(formattedDate); // 假设你有一个 `setTime` 方法来保存格式化后的日期

                tbCustomerOrder.setOrderTime(null);

                // 输出结果
                System.out.println("转换后的日期: " + formattedDate);
            }


            startPage();
            List<TbCustomerOrder> list = tbCustomerOrderService.selectTbCustomerOrderList(tbCustomerOrder);

            return getDataTable(list);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 导出订单管理列表
     */
    @RequiresPermissions("system:merchant_order:export")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbCustomerOrder tbCustomerOrder)
    {
        List<TbCustomerOrder> list = tbCustomerOrderService.selectTbCustomerOrderList(tbCustomerOrder);
        ExcelUtil<TbCustomerOrder> util = new ExcelUtil<TbCustomerOrder>(TbCustomerOrder.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @RequiresPermissions("system:merchant_order:query")
    @GetMapping(value = "/{orderNumber}")
    public AjaxResult getInfo(@PathVariable("orderNumber") Long orderNumber)
    {
        return success(tbCustomerOrderService.selectTbCustomerOrderByOrderNumber(orderNumber));
    }

    /**
     * 获取订单商品详细信息
     */
    @RequiresPermissions("system:merchant_order:query")
    @GetMapping(value = "/detail/{orderNumber}")
    public AjaxResult getInfoDetail(@PathVariable("orderNumber") String orderNumber)
    {
        return success(tbCustomerOrderService.selectTbCustomerOrderByOrderNumberDetail(orderNumber));
    }



    /**
     * 新增订单管理
     */
    @RequiresPermissions("system:merchant_order:add")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbCustomerOrder tbCustomerOrder)
    {
        System.out.println(tbCustomerOrder);
        return toAjax(tbCustomerOrderService.insertTbCustomerOrder(tbCustomerOrder));
    }

    /**
     * 修改订单管理
     */
    @RequiresPermissions("system:merchant_order:edit")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbCustomerOrder tbCustomerOrder)
    {
        return toAjax(tbCustomerOrderService.updateTbCustomerOrder(tbCustomerOrder));
    }

    /**
     * 删除订单管理
     */
    @RequiresPermissions("system:merchant_order:remove")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderNumbers}")
    public AjaxResult remove(@PathVariable Long[] orderNumbers)
    {
        return toAjax(tbCustomerOrderService.deleteTbCustomerOrderByOrderNumbers(orderNumbers));
    }
}
